#include "cjson.h"
#include "cjsonmake.h"
#include "stdlib.h"
#include "stdio.h"
/**********************************
启动文件中：默认最多只能解析三层键值项
修改为：Heap_Size EQU 0x00000C00    3 kb

{
	"iotId": "xLloL1lQxbbqksm4Hihd000100",
	"method": "thing.event.property.post",
	"params": {
			"MotorSpeed": -30
	  	"Humi": 50
	},
	"topic": "/sys/a1LDPqVpBCh/XYD_HOME/thing/event/property/post",
	"uniMsgId": "4818231753821577216",
	"version": "1.0"
}
**********************************/


char* MyCreatJson(TYPE_DHT11 dht11,int motor,char *RGB)
{
	char *string=NULL;
	//cJSON *newobj=NULL;
	cJSON *resolutions = NULL;
	cJSON *monitor = cJSON_CreateObject();
	
	/*在monitor对象中添加子条目("iotId": "xLloL1lQxbbqksm4Hihd000100"), 该条目为string类型*/
	resolutions=cJSON_AddStringToObject(monitor, "iotId", (char *)"xLloL1lQxbbqksm4Hihd000100");
	if(NULL==resolutions)
	{
			printf("cJSON_AddStringToObject failed\n");
			cJSON_Delete(monitor);
			return NULL;
	}
	/*在monitor对象中添加子条目("method": "thing.event.property.post"), 该条目为string类型*/
	resolutions=cJSON_AddStringToObject(monitor, "method", (char *)"thing.event.property.post");
	if(NULL==resolutions)
	{
			printf("cJSON_AddStringToObject failed\n");
			cJSON_Delete(monitor);
			return NULL;
	}
	
	/*在monitor对象中添加子条目("params"), 该条目为对象类型*/
	resolutions=cJSON_AddObjectToObject(monitor, "params");
	if(NULL==resolutions)
	{
		printf("cJSON_AddObjectToObject failed\n");
		cJSON_Delete(monitor);
		return NULL;
	}
	//*************************************************************
	
	cJSON_AddStringToObject(resolutions,"RGB",RGB);
	cJSON_AddNumberToObject(resolutions,"Temp",dht11.temp);
	cJSON_AddNumberToObject(resolutions,"Humi",dht11.humi);
	cJSON_AddNumberToObject(resolutions,"MotorSpeed",motor);

	//*************************************************************

	/*在monitor对象中添加子条目("topic": "/sys/a1LDPqVpBCh/XYD_HOME/thing/event/property/post"), 该条目为string类型*/
	resolutions=cJSON_AddStringToObject(monitor, "topic",(char *)"/sys/a1jwTHmfQCU/Intellig_Home/thing/event/property/post");
	if(NULL==resolutions)
	{
			printf("cJSON_AddStringToObject topic failed\r\n");
			cJSON_Delete(monitor);
			return NULL;
	}
	/*在monitor对象中添加子条目("uniMsgId": "4818231753821577216"), 该条目为string类型*/	
	resolutions=cJSON_AddStringToObject(monitor, "uniMsgId", (char *)"4818231753821577216");
	if(NULL==resolutions)
	{
			printf("cJSON_AddStringToObject uniMsgId failed\r\n");
			cJSON_Delete(monitor);
			return NULL;
	}
	/*在monitor对象中添加子条目("version": "1.0"), 该条目为string类型*/
	resolutions=cJSON_AddStringToObject(monitor, "version", (char *)"1.0");
	if(NULL==resolutions)
	{
			printf("cJSON_AddStringToObject version failed\r\n");
			cJSON_Delete(monitor);
			return NULL;
	}
	
	/*将JSON对象写入string对象中*/
	string = cJSON_Print(monitor);
	if(string == NULL){
		printf("Failed to print monitor.\r\n");
		cJSON_Delete(monitor);
		return NULL;
	}

	/*释放cJSON对象占用的资源*/
	cJSON_Delete(monitor);
	return string;
}
